﻿Namespace GUI

    Friend Class MirWindow

        Private Const cDragTreashold As Integer = 16

        Dim mName As String = ""

        Public mPosX As Integer, mPosY As Integer
        Dim mOriginX As Integer, mOriginY As Integer
        Dim mWidth As Integer, mHeight As Integer

        Dim mIsMoveable As Boolean
        Dim mDragX As Integer, mDragY As Integer

        Dim mClientRect As Rectangle

        'Dim lstButtons As New List(Of MirButton)
        'Dim lstTextBoxes As New List(Of MirTextBox)
        'Dim lstAnims As New List(Of MirAnimation)
        'Dim lstTexts As New List(Of MirText)

        Dim lstinterface As New List(Of Windows.GUIObj)

        Public Event DrawWindow(ByRef mode As DirectX.DrawingMode, ByRef x As Integer, ByRef y As Integer, ByRef z As Single)
        Public Event MouseEvent(ByRef type As Mouse.MouseInputType, ByRef e As MouseEventArgs)
        Public Event InterfaceMouseEvent(ByRef type As Mouse.MouseInputType, ByRef e As MouseEventArgs, ByRef InterfaceItem As Windows.GUIObj)

        Public Sub New(ByRef name As String, Optional ByRef x As Integer = 0, Optional ByRef y As Integer = 0, _
                        Optional ByRef width As Integer = 0, Optional ByRef height As Integer = 0, Optional ByRef ismoveable As Boolean = False)

            If name.Length > 0 Then

                mName = name
                mPosX = x
                mPosY = y
                mWidth = width
                mHeight = height
                mIsMoveable = ismoveable

                CalcClientRect()

            Else

                Throw New Exception("name must contain a valid string")

            End If

        End Sub

        Public Sub Destory()

            If lstinterface.Count > 0 Then
                For i As Integer = 0 To lstinterface.Count - 1
                    lstinterface(0).Destroy()
                Next
            End If

            Windows.RemoveWindow(Me)

        End Sub

        Public Sub MouseInput(ByRef type As Mouse.MouseInputType, ByRef e As MouseEventArgs)

            Dim ex As New MouseEventArgs(e.Button, e.Clicks, e.X - (mPosX - mOriginX), e.Y - (mPosY - mOriginY), e.Delta)

            Dim i As Integer

            If lstinterface.Count > 0 Then
                Dim c As Integer = lstinterface.Count - 1
                For i = c To 0 Step -1
                    If lstinterface(i).Contains(ex.X, ex.Y) Then
                        If type = Mouse.MouseInputType.MouseClick Then Windows.FocusWindow(Me) : Windows.FocusInterface(lstinterface(i))
                        lstinterface(i).MouseInput(type, ex)
                        RaiseEvent InterfaceMouseEvent(type, ex, lstinterface(i))
                        Exit Sub
                    End If
                Next
            End If

            'If lstButtons.Count > 0 Then
            'For i = lstButtons.Count - 1 To 0 Step -1
            'If lstButtons(i).Contains(ex.X, ex.Y) Then
            'lstButtons(i).MouseInput(type, ex)
            'If type = Mouse.MouseInputType.MouseClick Then Windows.FocusInterface(lstButtons(i))
            'Exit Sub
            'End If
            'Next
            'End If

            'If lstTextBoxes.Count > 0 Then
            'For i = lstTextBoxes.Count - 1 To 0 Step -1
            'If lstTextBoxes(i).Contains(ex.X, ex.Y) Then
            'lstTextBoxes(i).MouseInput(type, ex)
            'If type = Mouse.MouseInputType.MouseClick Then Windows.FocusInterface(lstTextBoxes(i))
            'Exit Sub
            'End If
            'Next
            'End If

            'If lstAnims.Count > 0 Then
            'For i = lstAnims.Count - 1 To 0 Step -1
            'If lstAnims(i).Contains(ex.X, ex.Y) Then
            'lstAnims(i).MouseInput(type, ex)
            'If type = Mouse.MouseInputType.MouseClick Then Windows.FocusInterface(lstAnims(i))
            'Exit Sub
            'End If
            'Next
            'End If

            'If lstTexts.Count > 0 Then
            'For i = lstTexts.Count - 1 To 0 Step -1
            'If lstTexts(i).Contains(ex.X, ex.Y) Then
            'lstTexts(i).MouseInput(type, ex)
            'If type = Mouse.MouseInputType.MouseClick Then Windows.FocusInterface(lstAnims(i))
            'Exit Sub
            'End If
            'Next
            'End If

            'mouse event is not hitting any interface item, message is for window
            If type = Mouse.MouseInputType.MouseClick Then
                Windows.FocusInterface(Nothing)
                Windows.FocusWindow(Me)
                'If Mouse.MovingObj Is Me Then Exit Sub
            End If

            If mIsMoveable And type = Mouse.MouseInputType.MouseDown Then
                mDragX = ex.X
                mDragY = ex.Y
                Windows.FocusInterface(Nothing)
                Windows.FocusWindow(Me)

                Mouse.MovingObj = Me

            End If

            If mIsMoveable And type = Mouse.MouseInputType.MouseMove And Mouse.bIsDown And Mouse.MovingObj Is Me Then

                Dim x As Integer = mPosX + (ex.X - mDragX)
                Dim y As Integer = mPosY + (ex.Y - mDragY)

                SetPosition(x, y)


            End If

            RaiseEvent MouseEvent(type, ex)

        End Sub

        Public Sub SetPosition(ByRef x As Integer, ByRef y As Integer)


            If x + mWidth > cDragTreashold And x < DirectX.DispMode.Width - cDragTreashold Then
                mPosX = x
            End If

            If y + mHeight > cDragTreashold And y < DirectX.DispMode.Height - cDragTreashold Then
                mPosY = y
            End If

            CalcClientRect()

        End Sub

        Public Sub SetOrigin(ByRef x As Integer, ByRef y As Integer)

            mOriginX = x
            mOriginY = y

            CalcClientRect()

        End Sub

        Public Sub SetSize(ByRef w As Integer, ByRef h As Integer)

            mWidth = w
            mHeight = h

            CalcClientRect()

        End Sub

        Private Sub CalcClientRect()

            mClientRect = New Rectangle(mPosX - mOriginX, mPosY - mOriginY, mWidth + mOriginX, mHeight + mOriginY)

        End Sub

        Public Function Contains(ByRef x As Integer, ByRef y As Integer) As Boolean

            Return mClientRect.Contains(x, y)

        End Function

        Public Sub Draw(ByRef mode As DirectX.DrawingMode, ByRef z As Single)

            RaiseEvent DrawWindow(mode, mPosX - mOriginX, mPosY - mOriginY, z)

            Dim i As Integer

            'draw interface
            If lstinterface.Count > 0 Then
                Dim c As Integer = lstinterface.Count - 1
                For i = 0 To c
                    lstinterface(i).Draw(mode, mPosX - mOriginX, mPosY - mOriginY, z)
                Next
            End If


            'draw interface
            'If lstButtons.Count > 0 Then
            'For i = 0 To lstButtons.Count - 1
            'lstButtons(i).Draw(mPosX - mOriginX, mPosY - mOriginY)
            'Next
            'End If

            'If lstTextBoxes.Count > 0 Then
            'For i = 0 To lstTextBoxes.Count - 1
            'lstTextBoxes(i).Draw(mPosX - mOriginX, mPosY - mOriginY)
            'Next
            'End If

            'If lstAnims.Count > 0 Then
            'For i = 0 To lstAnims.Count - 1
            'lstAnims(i).Draw(mPosX - mOriginX, mPosY - mOriginY)
            'Next
            'End If

            'If lstTexts.Count > 0 Then
            'For i = 0 To lstTexts.Count - 1
            'lstTexts(i).Draw(mPosX - mOriginX, mPosY - mOriginY)
            'Next
            'End If

        End Sub

        Public Sub AddInterfaceItem(ByRef item As Windows.GUIObj)

            lstinterface.Add(item)

        End Sub

        Public Sub RemoveInterfaceItem(ByRef item As Windows.GUIObj)

            lstinterface.Remove(item)

        End Sub

        'Public Sub AddInterfaceItem(ByRef item As MirButton)

        '           lstButtons.Add(item)

        '      End Sub

        '     Public Sub AddInterfaceItem(ByRef item As MirTextBox)

        '        lstTextBoxes.Add(item)

        '   End Sub

        '  Public Sub AddInterfaceItem(ByRef item As MirAnimation)

        '     lstAnims.Add(item)

        'End Sub

        'Public Sub AddInterfaceItem(ByRef item As MirText)

        '   lstTexts.Add(item)

        'End Sub

    End Class

End Namespace
